using UnityEngine;
using UnityEngine.UI;
using QFramework;
using QAssetBundle;

namespace QFramework.Example
{
    public class UIGamePanelData : UIPanelData
    {
    }
    public partial class UIGamePanel : UIPanel
    {

        protected override void OnInit(IUIData uiData = null)
        {
            mData = uiData as UIGamePanelData ?? new UIGamePanelData();
            // please add init code here
            ExpUpgradePanel.Hide();

            OnUIShow();
            //等级改变时发生的逻辑
            Global.Level.Register(level =>
            {
                ExpUpgradePanel.Show();
                if (level != 1)
                    AudioKit.PlaySound(Sfx.LEVELUP);
                Time.timeScale = 0;
            }).UnRegisterWhenGameObjectDestroyed(gameObject);

            //经验达到升级条件处理逻辑事件
            Global.Exp.RegisterWithInitValue(exp =>
            {
                if (exp >= Global.ExpToNextLevel())
                {
                    exp -= Global.ExpToNextLevel();
                    Global.Exp.Value = exp;
                    Global.Level.Value++;
                }
            }).UnRegisterWhenGameObjectDestroyed(gameObject);

            // OnAddAblilty();
            OnActionUpdate();



        }

        protected override void OnOpen(IUIData uiData = null)
        {
            UIKit.ClosePanel<UIGameWinPanel>();
            UIKit.ClosePanel<UIGameOverPanel>();
        }

        protected override void OnShow()
        {
        }

        protected override void OnHide()
        {
        }

        protected override void OnClose()
        {
        }



        /// <summary>
        /// 文本显示
        /// </summary>
        private void OnUIShow()
        {

            //经验增值事件
            Global.Exp.RegisterWithInitValue((exp) =>
            {
                expValue.fillAmount = exp / (float)Global.ExpToNextLevel();

            }).UnRegisterWhenGameObjectDestroyed(gameObject);

            //等级增值事件
            Global.Level.RegisterWithInitValue(level =>
            {
                TextLevel.text = "等级：" + level;
            }).UnRegisterWhenGameObjectDestroyed(gameObject);

            Global.EnemyCount.RegisterWithInitValue(count =>
            {
                TextEnemy.text = "怪物：" + count;
            }).UnRegisterWhenGameObjectDestroyed(gameObject);

            Global.EnemyWaveCount.RegisterWithInitValue(waveCount =>
            {
                TextEnemyWaveCount.text = "波次：" + waveCount;
            }).UnRegisterWhenGameObjectDestroyed(gameObject);

            //游戏时间
            Global.CurrentTime.RegisterWithInitValue(time =>
            {
                if (Time.frameCount % 30 != 0) return;

                float curTime = time;
                float second = Mathf.FloorToInt(curTime % 60);
                float minute = Mathf.FloorToInt(curTime / 60);
                TextTime.text = "时间：" + $"{minute:00}：{second:00}";
            }).UnRegisterWhenGameObjectDestroyed(gameObject);

            ////永久金币数据
            //Global.Coin.Value = PlayerPrefs.GetInt(Global.CoinStr.Value);
            //金币
            Global.Coin.RegisterWithInitValue(coin =>
            {
                TextCoin.text = "金币：" + Global.Coin.Value;
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
        }

        /// <summary>
        /// Update事件侦听
        /// </summary>
        private void OnActionUpdate()
        {

            var EnemyGre = FindAnyObjectByType<EnemyGenerator>();
            //Update
            ActionKit.OnUpdate.Register(() =>
            {
                Global.CurrentTime.Value += Time.deltaTime;
                if (EnemyGre.OnGetWaveIsOver() && EnemyGre.OnGetLastWaveOver() && Global.EnemyCount.Value <= 0)//通关条件
                {
                    Time.timeScale = 0;
                    AudioKit.PlaySound(Sfx.GAMEPASS);
                    EnemyGre.OnRefreshWaveCount();
                    UIKit.OpenPanel<UIGameWinPanel>();
                }
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
        }
    }
}
